package com.syqc.comlib.custom

import android.content.Context
import android.view.LayoutInflater
import android.view.View
import android.view.WindowManager
import android.widget.TextView
import androidx.appcompat.app.AlertDialog
import com.syqc.comlib.R

class MessageDialog(
    context: Context,
    negative: Int = 0,
    positive: Int = 0,
    clickListener: View.OnClickListener,
    isModify: Boolean = false
) :
    AlertDialog(context, R.style.GspTheme_Dialog) {

    private var tvPositive: TextView
    private var tvMessage: TextView

    init {
        val inflater = LayoutInflater.from(context).inflate(R.layout.dialog_message, null)
        val tvNegative: TextView = inflater.findViewById(R.id.tvNegative)
        tvMessage = inflater.findViewById(R.id.tvMessage)
        tvPositive = inflater.findViewById(R.id.tvPositive)
        tvPositive.setOnClickListener {
            dismissDialog()
            clickListener.onClick(it)
        }
        setCanceledOnTouchOutside(false)
        window?.decorView?.setPadding(0, 0, 0, 0)
        val attributes = window?.attributes
        attributes?.width = WindowManager.LayoutParams.MATCH_PARENT
        attributes?.height = WindowManager.LayoutParams.WRAP_CONTENT
        window?.attributes = attributes

        setView(inflater)
        if (positive != 0) {
            tvPositive.setText(positive)
        }
        if (negative != 0) {
            tvNegative.setText(negative)
        }
        if (isModify) {
            tvNegative.setOnClickListener(clickListener)
        } else {
            tvNegative.setOnClickListener { dismiss() }
        }

    }

    fun show(message: String) {
        tvMessage.text = message
        if (!isShowing) show()
    }


    fun show(messageId: Int) {
        tvMessage.text = context.getString(messageId)
        if (!isShowing) show()
    }

    fun dismissDialog() {
        if (isShowing) dismiss()
    }
}